#include "wwdg.h"

/**
 * @attention Tout_min(us)=(4096*预分频值)*(计数初值-窗口值+1)/APB1时钟频率(36MHz)
 * @attention Tout_max(us)=(4096*预分频值)*(计数初值-0x40+1)/APB1时钟频率(36MHz)
 * @attention Tout_min=30.947ms   Tout_max=50.062ms
 */
void WWDG_Init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
    WWDG_SetPrescaler(WWDG_Prescaler_8);
    WWDG_SetWindowValue(0x55); // 窗口值(0x40~0x7F)
    WWDG_NVIC_Init();          // WWDG中断分组初始化
    WWDG_ClearFlag();          // 清除提前唤醒中断标志位
    WWDG_EnableIT();           // 开启窗口看门狗中断
    WWDG_Enable(0x76);         // 计数初值7bit(0x40~0x7F)
}

void WWDG_NVIC_Init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = WWDG_IRQn;           // WWDG中断
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // 抢占2，子优先级3，组2
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;        // 抢占2，子优先级3，组2
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure); // NVIC初始化
}

void WWDG_Feed(void)
{
    WWDG_SetCounter(0x76); // 喂狗
}

/**
 * @attention 计数值到0x40 看门狗产生中断
 * @attention 计数值到0x3F 系统复位
 */
void WWDG_IRQHandler(void)
{
    WWDG_ClearFlag();
    // 复位前要做的任务.....
}
